--[[

	GModRP - Real Garry's Mod Roleplay.
			By Slob187.
		

]]			
   
--includes
include('shared.lua')
include('sv_ply_functions.lua')

--lua file download
AddCSLuaFile("shared.lua")
AddCSLuaFile("cl_init.lua")
AddCSLuaFile("itembase.lua")
AddCSLuaFile( "configuration.lua" )

--base include
for k, v in pairs(file.FindInLua("GModRP/gamemode/base/*.lua")) do

	include("base/"..v)
	
end


--plugins include
for k, v in pairs(file.FindInLua("GModRP/gamemode/sv_plugins/*.lua")) do

	include("sv_plugins/"..v)
	
end


--derma download
for k, v in pairs( file.FindInLua( "GModRP/gamemode/derma/*.lua" ) ) do

	AddCSLuaFile("derma/"..v)
	
end


--vgui download
for k, v in pairs( file.FindInLua( "GModRP/gamemode/vgui/*.lua" ) ) do

	AddCSLuaFile("vgui/"..v)
	
end

Radiostations = {}

function GMRP.AddMaterial( f )

	resource.AddFile( "materials/"..f..".vmt" )
	resource.AddFile( "materials/"..f..".vtf" )
	
end	


GMRP.AddMaterial( "GModRP/icons/health" )
GMRP.AddMaterial( "GModRP/icons/hunger" )
GMRP.AddMaterial( "GModRP/icons/sprint" )
GMRP.AddMaterial( "GModRP/icons/money" )


--settings
if ( Configuration["realistic"] == 1 ) then

	game.ConsoleCommand( "sbox_godmode 0\n" )
	game.ConsoleCommand( "sbox_plpldamage 0\n" )
	game.ConsoleCommand( "sbox_noclip 0\n" )
	
end	


if ( Configuration["economy"] == 1 ) then

	timer.Create( "GModRPEconomy", Configuration["economyinterval"], 0, function()
	
		for k, v in pairs( GMRP.Items ) do
		
			local newPrice = math.Round( v.Price + math.random( -10, 10 ) )
			
			if ( newPrice > 0 ) then
			
				v.Price = newPrice
				
				RunLuaAll( "GMRP.Items['"..k.."'].Price = "..newPrice )
				
			end

		end
		
	end )
	
end

	
function GM:PlayerSpawn( ply )

	if !ply:GetNWBool( "sleeping" ) then
	
		if ( game.GetMap() == "rp_oviscity_gmc4" ) then
	
			GMRP.OvisCitySpawns( ply )
		
		end	
		
		ply:SpawnProtection()

	end
	
	ply:ApplyPlayerSettings()
	
	ply:UnSpectate()
	
end



function GM:PlayerHurt( ply )

	self.BaseClass:PlayerHurt( ply )
	ply:RealisticDamage()
	
end	



function GMRP.SetModel( ply, command, args )

	for k, v in pairs( citizenmodels ) do
	
		if tostring( args[1] ) == v then
		
			if GMRP.UseLimit( ply ) then
				ply:SetPData( "rpmodel", args[1] )
				ply:SetModel( args[1] )
				ply:GreenNotify( "Player model changed to "..tostring( args[1] )..". This model will stay after rejoining." )	
			end	
			
		end
		
	end
	
end

GMRP.command( "setmodel", GMRP.SetModel )


function GM:PlayerDeathSound(ply)

	ply:EmitSound("vo/npc/Barney/ba_ohshit03.wav") --say 'oh shit' when player dies
	return true --disable the default beep sound	
	
end


function GM:DoPlayerDeath( p, attacker, dmginfo )

	p:RedNotify( "You got knocked out and lost "..CUR..Configuration["knockouttax"].."." )
	p:TakeMoney( Configuration["knockouttax"] )

	p:CreateRagdoll()
	
end


function GM:ShowTeam( p )

	p:ConCommand( "gm_rp_shopmenu" )
	
end


function GM:ShowHelp(ply)

	ply:ConCommand("gm_rp_motd")
	return false
	
end


function GM:PlayerInitialSpawn( ply )
	
	self.BaseClass:PlayerInitialSpawn( ply )
	
	ply:SetupAccount()

	ply:CorrectMoney()

	if ( ply:GetPData( cDN ) == nil ) then
	
		ply:SetPData( cDN, "" )
		
	end	
	
	GMRP.SendCompanyData( ply:GetPData( cDN ), ply )
	
	ply:ConCommand( "gm_rp_motd" )

	GMRP.SendInventory( ply )

	for k, v in pairs( Radiostations ) do
	
		ply:SendLua("table.insert( Radiostations, '"..tostring( v ).."' )")
		
	end	
	
	if ( ply:Nick() == "UNOWNABLE" ) then

		ply:Kick( "Please change your name and rejoin." )
		
	end	
	
	ply:SetTeam( job( "unemployed" ) )
	
	ply:applyPayDay()
	
end


function GM:PlayerDisconnected( ply )

	self.BaseClass:PlayerDisconnected( ply )
	
	ply:CleanStuff()
	
end


local NextFireSpreadTime = 0

function GM:Think()

	for _, ply in pairs( player.GetAll() ) do	
	
		if ply:Alive() then
	
			ply:staminaCheck()
			
			ply:CheckHunger()		
		
			if ( ply.arrested ) then
			
				if ply.arrester:IsValid() then
				
					if ply.arrester:Alive() then
				
						ply:SetVelocity( ( ply.arrester:GetPos() - ply:GetPos() ):Normalize() * 100 )
						
					end	
					
				end

			end
		
			if ply:GetNWBool( "ishigh" ) then
			
				if ( ply:GetVelocity():Length() > 0 ) then

					ply:SetVelocity( Vector( math.random( -50, 50 ), math.random( -50, 50 ), 0 ) )
					
				end	
				
			end	
			
		end	
		
	end

	
	if CurTime() > NextFireSpreadTime then
	
		for k, v in pairs( ents.GetAll() ) do
		
			if v:GetClass() == "prop_physics" || v:IsPlayer() then
		
				if v:IsOnFire() then
				
					v:SetColor( 0, 0, 0, 255 )
					
					for _, ent in pairs( ents.FindInSphere( v:GetPos(), math.random( 30, 100 ) ) ) do
					
						if ( !ent:IsOnFire() ) then
					
							if ( ent:IsPlayer() && ent:Alive() ) then
							
								ent:Ignite( 30, 0 )
								ent:SetColor( 100, 100, 100, 255 )
		
							elseif ( ent:GetClass() == "prop_physics" ) then
							
								ent:Ignite( 300, 0 )
								ent:SetColor( 0, 0, 0, 255 )
								
							end	
							
						end	
						
					end
					
				end
				
			end	
			
		end
		
		NextFireSpreadTime = CurTime() + 3
		
	end	
	
end	


function GM:PlayerShouldTakeDamage( victim, attacker )

	if attacker:IsValid() then
	
		local attClass = attacker:GetClass()
	
		if ( attClass == "prop_physics" ) then
		
			return false
	
		elseif attacker:IsPlayer() then
		
			if ( attacker:GetActiveWeapon():IsValid() ) then
		
				if attacker:GetActiveWeapon():GetClass() == "weapon_stunstick" then
					
					victim:SetVelocity( attacker:GetAimVector():GetNormalized() * 1000 )
					victim:TakeDamage( 5, attacker, attacker:GetActiveWeapon() )		
					
					return false		
					
				end	

			end
			
		end	
		
	end
	
	return true
	
end


function GM:GravGunPunt( userid, target )

	DropEntityIfHeld( target )
	
	return false
	
end	


concommand.Add( "gm_spare1", function( p )

	p:ConCommand( "gm_showspare1" )
	
end )	


concommand.Add( "gm_spare2", function( p )

	p:ConCommand( "gm_showspare2" )
	
end )
	
	
	
function GM:ShowSpare1( p )

	p:ConCommand( "gm_rp_playermenu" )
	
end


function RunLuaAll( lua )

	for k, v in pairs( player.GetAll() ) do
	
		v:SendLua( lua )
		
	end

end


function GM:KeyPress( ply, key )

	self.BaseClass:KeyPress( ply, key )

	local trace = {}
	trace.start = ply:EyePos()
	trace.endpos = trace.start + ply:GetAimVector() * 85
	trace.filter = ply
	local tr = util.TraceLine( trace )	
	
	if !tr.Entity:IsValid() then return end	
	
	if key == IN_USE && !ply:KeyDown( IN_SPEED ) then
		
		if tr.Entity.Money then
		
			ply:GreenNotify( "+"..CUR..tr.Entity.Amount )
			ply:AddMoney(tr.Entity.Amount )
			tr.Entity:Remove()
			
		end	
		
		if !tr.Entity.IsItem then return end
		
		tr.Entity.Use( ply, tr.Entity )
		
		if tr.Entity.Removeable then
		
			tr.Entity:Remove()
			
		end	

	elseif ( key == IN_RELOAD ) then
	
		if ( !ply:InVehicle() ) then
	
			if ( tr.Entity.IsItem || ( string.find( tr.Entity:GetClass(), "vehicle" ) && tr.Entity:GetNWString( "doorowner" ) == ply:Nick() ) ) then
			
				if string.find( tr.Entity:GetClass(), "vehicle" ) then
				
					ply.alreadyOwned = ply.alreadyOwned - 1
					
				end	
				
				--if it's a radiostation, delete the radiostation's name from radiostation list
				GMRP.DeleteRadiostation( tr.Entity )				
			
				GMRP.AddToInentory( ply, tr.Entity.ID )	
				tr.Entity:Remove()
				
			end
			
		end	

	end

end	


function GM:GetFallDamage( ply, fspeed )
 
	return ( fspeed / 8 )
 
end


function GM:CanPlayerSuicide( p )

	if p.arrested then
	
		p:PrintMessage( HUD_PRINTCONSOLE, "You can not suicide while handcuffed." )
		return false
		
	end	
	
	return true
	
end


function GM:PlayerSwitchFlashlight( ply, SwitchOn )
	
	return ply.hasFlashlight || !SwitchOn

end	


function GM:PlayerCanHearPlayersVoice( l, t )

	for k, v in pairs( ents.FindInSphere( t:GetPos(), Configuration["talkrange"] ) ) do
	
		if v == l then
		
			return true
			
		end

	end
	
	return false
	
end


function GM:EntityRemoved( ent )

	GMRP.DeleteRadiostation( ent )
	
end	
